﻿using System;
using System.Collections.Generic;
using System.Text;

using Oak.Core;
using Oak.Base.Logic;
using Oak.Model;

namespace Oak.Base
{
    public class OakSession : Session, ISession
    {

        private BsIAccountInfo account;
        private BsAdminInfo admin;

        public OakSession(string sessionKey) : base(sessionKey)
        {

            if (String.IsNullOrEmpty(sessionKey))
                return;

            BsAccountInfo acc = Get<BsAccountInfo>(sessionKey);
            if (acc == null)
                return;

            if (acc.AccountType.Value == AccountType.Admin)
            {
                AdminLG adminLG = new AdminLG();
                string msg;
                admin = adminLG.GetAdminById(acc.AdminId.Value, out msg);
                if (admin == null)
                    return;
                this.account = admin;
            }
        }

        public bool IsAdmin
        {
            get
            {
                if (!this.IsLogin)
                    return false;
                return account.AccountType.Value == AccountType.Admin;
            }
        }

        public bool IsSuperAdmin
        {
            get
            {
                if (!IsAdmin)
                    return false;
                return admin.IsSuperAdmin.Value;
            }
        }

        public BsAdminInfo Admin
        {
            get { return admin; }
        }

        public AccountType AccessType
        {
            get { return account.AccountType.Value; }
        }

        public BsIAccountInfo Account
        {
            get { return account; }
        }

        public long AccountId
        {
            get
            {
                if (this.account.AccountType.Value == AccountType.Admin && admin != null)
                    return admin.AdminId.Value;

                return -1;
                
            }
        }

        public bool IsLogin
        {
            get
            {
                if (this.account == null)
                    return false;

                if (this.account.AccountType.Value == AccountType.Admin && admin != null)
                    return true;

                return false;
            }
        }



    }
}
